// pages/creat/creat.js
const common = require('../../mixins/common')
const forum = require('../../mixins/forum')
import config from "../../api/config";
import custom from "../../api/custom";
var app = getApp();

const options = {

  /**
   * 页面的初始数据
   */
  data: {
    tabbar: config.tarbar,
    currentConfig: config.currentConfig,
    type: 'forums',
    innerAudioContext: null,
    showNetDisk: false,
    isAddNetDisk: false,
    menus: [{
        id: 0,
        icon: "/image/edit_page_upload_pictures.png",
        text: "图片"
      }, {
        id: 1,
        icon: "/image/edit_page_upload_video.png",
        text: "视频"
      }
      // {
      //   id: 2,
      //   icon: "/image/edit_page_upload_bigvideo.png",
      //   text: "长视频"
      // }, {
      //   id: 3,
      //   icon: "/image/edit_page_upload_record.png",
      //   text: "音频"
      // }, {
      //   id: 4,
      //   icon: "/image/edit_page_upload_record_copy.png",
      //   text: "附件"
      // },
      // {
      //   id: 5,
      //   icon: "/image/edit_upload_wangpan.png",
      //   text: "网盘"
      // }
    ],
    custom: [],
    mediaType: -1,
    pictureMax: 9,
    videoDuration: 60,
    imagesubject: [],
    mask: false,
    inputLink: "",
    safeAreaMarginBottom: 0,
    defaultVoiceTime: 300,
    format: 'standard',
    currentConfig: config.currentConfig,
    tags: [],
    NetDiskLink: [{
      link: '',
      psw: '',
    }],
    cats: [],
    // objectArray: [{
    //     name: '自定义字段'
    //   },
    //   {
    //     name: '二手交易',
    //   },
    //   {
    //     name: '招聘信息',
    //   },
    //   {
    //     name: '活动信息'
    //   }
    // ],
    objectIndex: 0,
    anonymity: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (options.topic) {
      this.setData({
        topicids: options.topicid,
        topic: options.topic,
      })
    }
    /**
     * 获取快速选择菜单
     */
    this.getCircleLists({
      'custom': 'sticky'
    });
  },

  /**
   * 取消发布
   */

  onCancelButton: function () {
    wx.navigateBack({
      delta: 0,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.initInnerAudioContext();
  },

  /**
   * 快速选择圈子
   */
  selectTopic: function (e) {
    let name = e.currentTarget.dataset.name;
    let id = e.currentTarget.id;
    this.setData({
      topic: name,
      topicid: id,
    });
  },

  /**
   * 监听网盘链接输入
   */
  onEditInputChange: function (t) {
    this.setData({
      inputLink: t.detail.value
    });
  },
  /**
   * 监听长视频链接输入
   */
  onConfirmButton: function () {
    if (this.data.inputLink == '') wx.showToast({
      title: '请添加视频链接'
    })
    else {
      var i = this.data.inputLink;
      this.jumpAnalysis({
        url: i
      })
      this.setData({
        showBigVideoEdit: !1,
        inputLink: ""
      });
    }
  },
  /**
   * 自定义字段选择
   */
  bindPickerChange2: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      objectIndex: e.detail.value
    })
    let name = this.data.objectArray[e.detail.value].name;
    switch (name) {
      case '自定义字段':
        this.setData({
          custom: [{
            name: '',
            value: ''
          }]
        })
        break;
      case '二手交易':
        this.setData({
          custom: custom.ershou,
        })
        break;
      case '招聘信息':
        this.setData({
          custom: custom.zhaopin,
        })
        break;
      case '活动信息':
        this.setData({
          custom: custom.huodong,
        })
        brea

    }
  },
  /**
   * 取消网盘链接
   */
  close: function () {
    this.setData({
      show: !1,
      inputLink: ""
    })
  },
  /**
   * 取消网盘链接
   */
  colseNetDisk: function () {
    this.setData({
      showNetDisk: false,
    })
  },
  /**
   * 是否匿名
   */
  changeSwitch: function (e) {
    console.log(e)
    let value = e.detail.value;
    this.setData({
      anonymity: value
    })
  },
  /**
   * 网盘链接监听
   */
  onEditNetDiskLink: function (t) {
    let NetDiskLink = t.detail.value;
    this.data.NetDiskLink[0].link = NetDiskLink;
    this.setData({
      NetDiskLink: this.data.NetDiskLink
    });
  },
  /**
   * 网盘提取码监听
   */
  onEditNetDiskPsw: function (t) {
    let NetDiskPsw = t.detail.value;
    this.data.NetDiskLink[0].psw = NetDiskPsw;
    this.setData({
      NetDiskLink: this.data.NetDiskLink
    });
  },

  onConfirmDiskButton: function () {
    this.setData({
      isAddNetDisk: true,
      showNetDisk: false,
    })
  },
  /**
   * 位置选择
   */
  onLocationClick: function () {
    var t = this;
    this.checkPermission("scope.userLocation", "请到设置页面授权“位置信息”权限", function () {
      t.startChooseLocation();
    });
  },

  /**
   * 视频删除
   */
  onVideoDelete: function () {
    this.setData({
      mediaType: -1,
      video: '',
      format: 'standard',
      imagesubject: [],
    })
  },
  /**
   * 音频删除
   */
  onAudioDelete: function () {
    this.setData({
      mediaType: -1,
      audioInfo: [],
      format: 'standard',
      imagesubject: [],
    })
  },
  /**
   * 文档删除
   */
  onAttachmentDelete: function () {
    this.setData({
      mediaType: -1,
      attachmentInfo: [],
      format: 'standard',
      imagesubject: [],
    })
  },
  /**
   * 删除自定义字段
   */
  deleteCustom: function (t) {
    var e = t.currentTarget.dataset.index,
      a = this.data.custom;
    a.splice(e, 1);
    this.setData({
      custom: a
    });

  },

  startChooseLocation: function () {
    var t = this;
    wx.chooseLocation({
      success: function (a) {
        t.setData({
          location: {
            latitude: a.latitude,
            longitude: a.longitude,
            address: a.address,
            name: a.name
          }
        });
      }
    });
  },
  /**
   * 监听菜单选择
   */
  onMenuItem: function (t) {
    var that = this;
    if (!this.data.user) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    var i = t.currentTarget.dataset.index,
      a = this.data.menus[i];
    switch (a.id) {
      case 0:
        this.setData({
          format: 'standard',
        })
        this.addPictures();
        break;

      case 1:
        this.addVideo();
        this.setData({
          format: 'video',
        })
        break;

      case 2:
        this.setData({
          format: 'video',
          mask: true,
          showBigVideoEdit: !0
        });
        break;

      case 3:
        var o = this;
        wx.showActionSheet({
          itemList: ["录制", "从会话选择"],
          itemColor: "#333333",
          success: function (t) {
            var i = t.tapIndex;
            o.selectAudio(i);
          }
        });
        break;

      case 4:
        this.addAttachment();
        break;
      case 5:
        wx.getClipboardData({
          success: (option) => {

            var reg = /链接：(.*)/;
            var link = reg.exec(option.data)[1].trim();
            var reg1 = /提取码：(.*)/;
            var key = reg1.exec(option.data)[1].trim();
            if (link != null) {
              wx.showModal({
                title: '检测到网盘链接',
                content: '是否直接导入',
                showCancel: true, //是否显示取消按钮
                cancelText: "取消", //默认是“取消”
                confirmText: "导入", //默认是“确定”
                success: function (res) {
                  if (res.cancel) {
                    //点击取消,默认隐藏弹框
                  } else {
                    //点击确定
                    var NetDiskLink = [{
                      link: link,
                      psw: key,
                    }]
                    that.setData({
                      NetDiskLink: NetDiskLink
                    })
                  }
                },
                fail: function (res) {}, //接口调用失败的回调函数
                complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
              })
            }
          },
        })
        this.setData({
          showNetDisk: true,
        })

        break;

      default:
        console.log("选择错误")
    }
  },
  /**
   * 输入自定义名称
   */
  inputName: function (t) {
    var index = t.currentTarget.dataset.index,
      custom = this.data.custom;
    custom[index].name = t.detail.value, this.setData({
      custom: custom
    });
  },
  /**
   * 输入自定义内容
   */
  inputValue: function (t) {
    var index = t.currentTarget.dataset.index,
      custom = this.data.custom;
    custom[index].value = t.detail.value, this.setData({
      custom: custom
    });

  },


  /**
   * 新增自定义选项
   */
  addCustom: function () {
    var custom = this.data.custom;
    if (custom.length > 20) d.showModelTips("最多允许创建20个选项");
    else {
      custom.push({
        name: "",
        value: "",
      }), this.setData({
        custom: custom
      });
    }
  },

  /**
   * 关闭长视频
   */

  close: function () {
    this.setData({
      showBigVideoEdit: !1,
      inputLink: ""
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let user = app.globalData.user
    if (!user) {
      wx.showToast({
        title: '未登录,请登录',
      })
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    this.setData({
      user: user,
    })
  },


  onTagClick: function () {
    wx.navigateTo({
      url: '/pages/creat/tags?tags=' + JSON.stringify(this.data.tags)
    })
  },

  /**
   * 新增自定义选项
   */
  onShopClick: function () {
    wx.navigateTo({
      url: '/pages/creat/shoplist'
    })
  },
  /**
   * 动态输入监听
   */

  onContentInput: function (t) {
    var i = t.detail.value;
    if (i.length > 0) {
      this.setData({
        textinput: i
      })
    }
  },

  /**
   * 选择圈子
   */
  onTopicClick: function () {
    wx.navigateTo({
      url: '/pages/creat/circle',
    })

  },

  /**
   * 删除图片
   */
  onPictureDelete: function (t) {
    var a = t.currentTarget.dataset.index;
    this.data.imagesubject.splice(a, 1), 0 == this.data.imagesubject.length && (this.data.mediaType = -1),
      this.setData({
        mediaType: this.data.mediaType,
        imagesubject: this.data.imagesubject
      });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}
common(options);
forum(options);
Page(options);